package com.qf.model.controller;

import com.qf.device.entity.DeviceType;
import com.qf.device.service.DeviceTypeService;
import com.qf.dto.PageDto;
import com.qf.model.entity.ModelTemplate;
import com.qf.model.mapper.ModelTemplateMapper;
import com.qf.model.service.ModelTemplateService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/modelTemplate")
@CrossOrigin
public class TemplateControllerModel {

    @Autowired
    private ModelTemplateMapper modelTemplateMapper;

    @Autowired
    private ModelTemplateService modelTemplateService;

    @Autowired
    private DeviceTypeService deviceTypeService;

    @GetMapping("/list")
    public Result list(){
        final List<ModelTemplate> modelTemplateList = modelTemplateMapper.findAll1();
        return new  Result(0,"success",modelTemplateList);
    }

    /*分页查询*/
    @PostMapping("page")
    public PageResult<ModelTemplate> page(@RequestBody PageDto<ModelTemplate> pageDto){
        return modelTemplateService.pageInfo(pageDto);
    }

    /*所有设备类型查询*/
    @GetMapping("type")
    public List<DeviceType> type(){
        return deviceTypeService.list();
    }

    /*添加和修改*/
    @PostMapping("add")
    public Result add(@RequestBody ModelTemplate modelTemplate){
        if (StringUtils.isEmpty(modelTemplate.getId())){
            return modelTemplateService.add(modelTemplate);
        }else{
            if(modelTemplateService.updateById(modelTemplate)){
                return new Result(0,"更新成功！");
            }else{
                return new Result(1,"更新失败！");
            }
        }
    }
}
